﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Reflection;
using Excel = Microsoft.Office.Interop.Excel;

namespace RBTools.Performances
{
    public partial class PerformanceForm : Form
    {
        #region Ctor

        public PerformanceForm()
        {
            InitializeComponent();
        }

        #endregion

        #region Events

        private void SelectDllFileButton_Click(object sender, EventArgs e)
        {
            if (DllFileOpenFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                DllFileTextBox.Text = DllFileOpenFileDialog.FileName;

                if (File.Exists(DllFileTextBox.Text))
                {
                    try
                    {
                        Assembly assembly = Assembly.LoadFrom(DllFileTextBox.Text);

                        ClassNameComboBox.Items.Clear();
                        StaticMethodNameComboBox.Items.Clear();

                        Type[] allTypes = assembly.GetTypes();
                        foreach (var type in allTypes)
                        {
                            if (type.GetMethods().FirstOrDefault(m => m.IsStatic) != null)
                                ClassNameComboBox.Items.Add(type);
                        }

                        if (ClassNameComboBox.Items.Count > 0)
                        {
                            ClassNameComboBox.SelectedIndex = 0;
                        }

                    }
                    catch (Exception error)
                    {
                    }
                }
            }
        }

        #endregion

        #region Form Load

        private void PerformanceForm_Load(object sender, EventArgs e)
        {
            ClassNameComboBox.DisplayMember = "Name";
            //ClassNameComboBox.ValueMember = "Type";
            StaticMethodNameComboBox.DisplayMember = "Name";
            //StaticMethodNameComboBox.ValueMember = "MethodInfo";
             
        }

        #endregion

        private void ExcuteAndGenerateToExcelButton_Click(object sender, EventArgs e)
        {
            if (StaticMethodNameComboBox.SelectedIndex >= 0 && ClassNameComboBox.SelectedIndex >= 0)
            {
                MethodInfo method = (MethodInfo)StaticMethodNameComboBox.SelectedItem;
                if (method != null)
                {
                    Dictionary<int, long> countTimeDict = new Dictionary<int, long>();
                    int seed = Convert.ToInt32(CountSeedTextBox.Text);
                    int increasement = Convert.ToInt32(IncreasementTextBox.Text);
                    int repeatCount = Convert.ToInt32(RepeatCountTextBox.Text);
                    for(int i=0;i<repeatCount;i++)
                    {
                        int count = seed+i*increasement;
                        long time = PerformanceHelper.Excute(method,count);
                        countTimeDict.Add(count, time);
                    }

                    Excel.Application excel = new Excel.ApplicationClass();
                    Excel.Workbook workbook = excel.Workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);
                    Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Sheets[1];
                     
                    ((Excel.Range)worksheet.Cells[1, 2]).Value2 = "Time";

                    for (int i = 0; i < repeatCount; i++)
                    { 
                        int count = seed+i*increasement;
                        ((Excel.Range)worksheet.Cells[i + 2, 1]).Value2 = count.ToString()+"c";
                        ((Excel.Range)worksheet.Cells[i + 2, 2]).Value2 = countTimeDict[count];
                    }
                    Excel.Range range = worksheet.get_Range(worksheet.Cells[1, 1], worksheet.Cells[repeatCount + 1, 2]);
                    range.Select();
                    Excel.ChartObjects chartObjects = (Excel.ChartObjects)worksheet.ChartObjects(Type.Missing);
                    Excel.ChartObject chartObj = chartObjects.Add(100, 100, 200, 200);
                    Excel.Chart chart = chartObj.Chart;
                    chart.ChartWizard(range, Excel.XlChartType.xlLineStacked, 0);

                    //Excel.Chart chart = (Excel.Chart)workbook.Charts.Add(Type.Missing,Type.Missing,Type.Missing,Excel.XlChartType.xlLineStacked); 

                    string fileName = "E:\\a.xlsx";
                    if (File.Exists(fileName))
                        File.Delete(fileName);
                    
                    workbook.SaveAs("E:\\a.xlsx");

                    excel.Quit();
                }
            }
            else
            {
                MessageBox.Show("Please select Class Name and Static Method before excute!");
            }
        }

        private void ClassNameComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            StaticMethodNameComboBox.Items.Clear();
            if (ClassNameComboBox.SelectedIndex >= 0)
            {
                Type type = (Type)ClassNameComboBox.SelectedItem; 
                MethodInfo[] methods = type.GetMethods();
                foreach (var method in methods)
                {
                    if (method.IsStatic)
                    {
                        StaticMethodNameComboBox.Items.Add(method);
                    }
                }
            }
        }

        private void StaticMethodNameComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

    }
}
